package com.xiaodu.acer.phoneplay.ui.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ms.square.android.expandabletextview.ExpandableTextView;
import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.AppInfo;
import com.xiaodu.acer.phoneplay.common.Constant;
import com.xiaodu.acer.phoneplay.widget.DownloadButtonConntroller;
import com.xiaodu.acer.phoneplay.widget.DownloadProgressButton;

import library.imageloader.ImageLoader;
import zlc.season.rxdownload2.RxDownload;


public class AppInfoAdapter extends BaseQuickAdapter<AppInfo, BaseViewHolder> {

    private Builder mBuilder;

    private DownloadButtonConntroller mDownloadButtonConntroller;

    private AppInfoAdapter(Builder builder) {
        super(builder.layoutId);
        this.mBuilder = builder;
        mDownloadButtonConntroller = new DownloadButtonConntroller(builder.mRxDownload);
        openLoadAnimation();
    }


    public static Builder builder() {
        return new Builder();
    }

    @Override
    protected void convert(BaseViewHolder helper, AppInfo item) {
        ImageView view = helper.getView(R.id.img_app_icon);
        ImageLoader.load(Constant.BASE_IMG_URL + item.getIcon(), view);
        helper.setText(R.id.txt_app_name, item.getDisplayName());

        helper.addOnClickListener(R.id.btn_download);


        View viewBtn = helper.getView(R.id.btn_download);
        //防止button复用时状态跟着变
        if (item.isFlag()) {
            viewBtn.setVisibility(View.VISIBLE);
        }/* else {
            viewBtn.setVisibility(View.GONE);
        }*/

        TextView textViewBrief = helper.getView(R.id.txt_brief);
        //升级在UpgradeAppFragment中
        if (mBuilder.isUpdateStatus) {
            ExpandableTextView viewChangeLog = helper.getView(R.id.view_changelog);
            viewChangeLog.setText(item.getChangeLog());
            if (textViewBrief != null) {
                textViewBrief.setVisibility(View.VISIBLE);
                textViewBrief.setText("v" + item.getVersionName() + "  " + (item.getApkSize() / 1014 / 1024) + "Mb");
            }
            if (viewBtn instanceof DownloadProgressButton) {
                DownloadProgressButton btn = (DownloadProgressButton) viewBtn;
                btn.setText("升级");
            }

        } else {
            TextView txtViewPosition = helper.getView(R.id.txt_position);
            if (txtViewPosition != null) {
                txtViewPosition.setVisibility(mBuilder.isShowPosition ? View.VISIBLE : View.GONE);
                txtViewPosition.setText((item.getPosition() + 1) + " .");
            }

            TextView textViewCategoryName = helper.getView(R.id.txt_category);
            if (textViewCategoryName != null) {
                textViewCategoryName.setVisibility(mBuilder.isShowCategoryName ? View.VISIBLE : View.GONE);
                textViewCategoryName.setText(item.getLevel1CategoryName());
            }
            if (textViewBrief != null) {
                textViewBrief.setVisibility(mBuilder.isShowBrief ? View.VISIBLE : View.GONE);
                textViewBrief.setText(item.getBriefShow());
            }
            // FIXME: 2018/1/9 txt_apk_size不在一个布局中
            TextView textViewSize = helper.getView(R.id.txt_apk_size);
            if (textViewSize != null) {
                textViewSize.setText((item.getApkSize() / 1014 / 1024) + "Mb");
            }
            if (viewBtn instanceof DownloadProgressButton) {
                DownloadProgressButton btn = (DownloadProgressButton) viewBtn;
                mDownloadButtonConntroller.handClick(btn, item);

            }


        }


    }

    public static class Builder {
        private boolean isShowPosition;
        private boolean isShowCategoryName;
        private boolean isShowBrief;
        private boolean isUpdateStatus;

        private RxDownload mRxDownload;


        private int layoutId = R.layout.template_appinfo;


        public Builder showPosition(boolean b) {

            this.isShowPosition = b;
            return this;
        }


        public Builder showCategoryName(boolean b) {

            this.isShowCategoryName = b;
            return this;
        }


        public Builder showBrief(boolean b) {
            this.isShowBrief = b;
            return this;
        }

        public AppInfoAdapter build() {
            return new AppInfoAdapter(this);
        }


        public Builder layout(int resId) {
            this.layoutId = resId;
            return this;
        }

        public Builder rxDownload(RxDownload rxDownload) {
            this.mRxDownload = rxDownload;
            return this;
        }

        public Builder updateStatus(boolean b) {
            this.isUpdateStatus = b;
            return this;
        }


    }


}
